﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Back.Xinkui.WebModel;
using System.Text;
using CommonFramework.Storage.Cache;
using CommonFramework.Storage.Cookie;
using CommonFramework.Utility;
using Back.Xinkui.Entity.Manager;
using Back.Xinkui.DataAccess.Manager;
using CommonFramework.Entity;
using Back.Xinkui.Common.Const;

public partial class Login : PageAspxBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (UserTicket.UserID != 0)
            {
                Response.Redirect("/main.aspx");
            }
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        //int intAppID = ConvertHelper.ToType<int>(DropApp.SelectedValue);
        CheckUserNameAndPassword(txtUserName.Text, txtPassWord.Text);
    }

    protected void CheckUserNameAndPassword(string UserName, string Password)
    {
        if (string.IsNullOrEmpty(UserName))//如果用户名为空
        {
            lbMsg.Text = "用户名为空";
        }
        else if (string.IsNullOrEmpty(Password))//如果密码为空
        {
            lbMsg.Text = "密码为空";
        }
        else
        {
            Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");
            VManagerUserEntity userentity = ManagerUserDataAccess.GetUserByUserName(UserName, Password);
            if (userentity == null)
            {
                lbMsg.Text = "用户名或密码不正确!";
            }
            else
            {
                if (userentity.IsActive == 1)
                {
                    lbMsg.Text = "此用户用已被锁定!";
                }
                else
                {
                    LoginByUserID(userentity.UserID);
                }
            }
        }
    }
    protected void LoginByUserID(int UserID)
    {
        StringBuilder HtmlStr = new StringBuilder();
        string ModuleMiUserID = SecurityHelper.Encrypt(UserID.ToString(), VerifyManageKey.GASGOO_VERIFY_KEY, DateTime.Now.ToString("yyyyMMdd"));
        CookieManager.Set(VerifyManageKey.GASGOO_VERIFYMANAGE_USERID, ModuleMiUserID);
        CacheManager.CreateCache(VerifyManageKey.GASGOO_VERIFY_APPID).Set<int>(WebHelper.UserIP, VerifyManageKey.MainAppID);
        Response.AddHeader("P3P", "CP=CAO PSA OUR");
        Response.Redirect("/main.aspx");

    }
}